home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / DEMO_VGA / FRSTM1.LZH / CHKPOINT.ASM < prev    next >
Assembly Source File  |  1989-03-27  |  16KB  |  642 lines

  1. ; Malder.asm by Mark C. Peterson, CompuServe [70441,3353]
  2.  
  3. _TEXT SEGMENT BYTE PUBLIC 'CODE'
  4. ASSUME CS:_TEXT
  5.  
  6. ; int DrawMandelbrot(long it, long jt, int Iter, int RequiredRep,
  7. ;                   long AspectX, long AspectY, int PointsPerLine,
  8. ;                   int ScreenLines, int OverflowMask, int SigDigits,
  9. ;                   int BytesPerLine, Initx, int Inity, int BitInc,
  10. ;                   int OffsetInc, struct _MALSET far *Malset, 
  11. ;                   char far *ColorLog, int Buttons, int Direct, 
  12. ;                   int InSetMask, int JuliaFlag, long Creal, long Cimag)
  13.  
  14. ; Passed variables
  15. it             equ      WORD PTR [bp + 6]
  16. jt             equ      WORD PTR [bp + 10]
  17. Iter           equ      WORD PTR [bp + 14]
  18. RequiredRep    equ      WORD PTR [bp + 16]
  19. AspectX        equ      WORD PTR [bp + 18]
  20. AspectY        equ      WORD PTR [bp + 22]
  21. PointsPerLine  equ      WORD PTR [bp + 26]
  22. ScreenLines    equ      WORD PTR [bp + 28]
  23. OverflowMask   equ      WORD PTR [bp + 30]
  24. SigDigits      equ      WORD PTR [bp + 32]
  25. BytesPerLine   equ      WORD PTR [bp + 34]
  26. Initx          equ      WORD PTR [bp + 36]
  27. Inity          equ      WORD PTR [bp + 38]
  28. BitInc         equ      WORD PTR [bp + 40]
  29. OffsetInc      equ      WORD PTR [bp + 42]
  30. Mousex         equ      WORD PTR [bp + 44]
  31. Mousey         equ      WORD PTR [bp + 46]
  32. Malset         equ      WORD PTR [bp + 48]
  33. ColorLog       equ      WORD PTR [bp + 52]
  34. Buttons        equ      WORD PTR [bp + 56]
  35. Direct         equ      WORD PTR [bp + 58]
  36. InSetMask      equ      WORD PTR [bp + 60]
  37. JuliaFlag      equ      WORD PTR [bp + 62]
  38. Creal          equ      WORD PTR [bp + 64]
  39. Cimag          equ      WORD PTR [bp + 68]
  40.  
  41. ; Local variables
  42. temp        equ      WORD PTR [bp - 8]
  43. sign        equ      BYTE PTR [bp - 10]
  44.  
  45. ; Pattern recognition variables
  46. Lasti       equ      WORD PTR [bp - 14]
  47. Lastj       equ      WORD PTR [bp - 18]
  48. LastRep     equ      WORD PTR [bp - 20]
  49. NumRep      equ      WORD PTR [bp - 22]
  50. CheckEvery  equ      WORD PTR [bp - 24]
  51. LastIter    equ      WORD PTR [bp - 26]
  52. LowMask     equ      WORD PTR [bp - 28]
  53. HighMask    equ      WORD PTR [bp - 30]
  54.  
  55. ; Other local variables
  56. iInit       equ      WORD PTR [bp - 34]
  57. jInit       equ      WORD PTR [bp - 38]
  58. Iterations  equ      WORD PTR [bp - 40]
  59. LinePoints  equ      WORD PTR [bp - 42]
  60. BitMask     equ      BYTE PTR [bp - 44]
  61. Offset      equ      WORD PTR [bp - 46]
  62. Plane       equ      BYTE PTR [bp - 48]
  63. LastCheck   equ      WORD PTR [bp - 52]
  64. LastLocal   equ      52
  65.  
  66. ; Declared Constants
  67. GraphContPort     equ      03ceh
  68.  
  69. PLANE_SELECT   equ   0b2h
  70. PLANE_MASK     equ   0e1h
  71.  
  72. GetATI_REG     PROC     NEAR
  73.          push  si
  74.          push  es
  75.          mov   ax, 0c000h
  76.          mov   es, ax
  77.          mov   si, 10h
  78.          mov   dx, es:[si]
  79.          pop   es
  80.          pop   si
  81.          ret
  82. GetATI_REG     ENDP
  83.  
  84. ; int GetATIConfig(void)
  85.  
  86. PUBLIC _GetATIConfig
  87. _GetATIConfig     PROC     FAR
  88.          call  GetATI_REG
  89.          cli
  90.          mov   al, 0bbh
  91.          out   dx, al
  92.          inc   dx
  93.          in    al, dx
  94.          sti
  95.          xor   ah, ah
  96.          ret
  97. _GetATIConfig     ENDP
  98.  
  99.  
  100. ATIPlane         PROC     NEAR   ; Plane number in ch
  101.          call  GetATI_REG
  102.          cli
  103.          mov   al, PLANE_SELECT
  104.          out   dx, al
  105.          inc   dl
  106.          in    al, dx
  107.          mov   ah, al
  108.          and   ah, PLANE_MASK
  109.          shl   ch, 1
  110.          or    ah, ch
  111.          mov   al, PLANE_SELECT
  112.          dec   dl
  113.          out   dx, ax
  114.          sti
  115.          ret
  116. ATIPlane         ENDP
  117.  
  118.  
  119. ; void SetATIPlane(int Plane)
  120.  
  121. extrn _VgaWonder:WORD
  122.  
  123. PUBLIC _SetATIPlane
  124. _SetATIPlane      PROC     FAR
  125.       push  bp
  126.       mov   bp, sp
  127.       mov   ax, _VgaWonder
  128.       or    ax, ax
  129.       jz    ExitSetATIPlane
  130.  
  131.       mov   cx, WORD PTR [bp + 6]
  132.       mov   ch, cl
  133.       call  ATIPlane
  134.  
  135. ExitSetATIPlane:
  136.       pop   bp
  137.       ret
  138. _SetATIPlane      ENDP
  139.  
  140.  
  141. SetATIPlane    PROC     NEAR
  142.       mov   ch, Plane
  143.       jmp   ATIPlane
  144. SetATIPlane    ENDP
  145.  
  146. ; void far _MouseRoutine(int x, int y)
  147. extrn _MouseRoutine:FAR
  148.  
  149. CheckMouse     PROC     NEAR
  150.       push  ax
  151.       push  bx
  152.       push  cx
  153.       push  dx
  154.       push  si
  155.       push  di
  156.       push  es
  157.       push  ds
  158.       mov   ax, 3
  159.       int   33h
  160.       cmp   cx, Mousex
  161.       jne   CallMouse
  162.       cmp   dx, Mousey
  163.       jne   CallMouse
  164.       cmp   bx, Buttons
  165.       je    ExitCheckMouse
  166.  
  167. CallMouse:
  168.       push  dx
  169.       push  cx
  170.       push  Malset + 2
  171.       push  Malset
  172.       call  _MouseRoutine
  173.       call  SetATIPlane
  174.       pop   ax
  175.       pop   ax
  176.       pop   Mousex
  177.       pop   Mousey
  178.  
  179. ExitCheckMouse:
  180.       pop   ds
  181.       pop   es
  182.       pop   di
  183.       pop   si
  184.       pop   dx
  185.       pop   cx
  186.       pop   bx
  187.       pop   ax
  188.       ret
  189. CheckMouse     ENDP
  190.  
  191. SetOffset   PROC     NEAR
  192.       mov   ax, Inity               ; Calculate initial offset and bit mask
  193.       mul   BytesPerLine
  194.       mov   bx, Direct
  195.       or    bx, bx
  196.       jz    PlanerOffset
  197.       add   ax, Initx
  198.       mov   Offset, ax
  199.       jmp   ExitSetOffset
  200.  
  201. PlanerOffset:
  202.       mov   bx, Initx
  203.       mov   cx, 3
  204.       shr   bx, cl
  205.       add   ax, bx
  206.       mov   Offset, ax              ; Offset = (y * BPL) + (x / 8)
  207.       mov   cx, Initx
  208.       and   cl, 7
  209.       mov   bl, 80h
  210.       shr   bl, cl                  ; BitMask = 0x80 >> (x & 7)
  211.       mov   BitMask, bl
  212.  
  213. ExitSetOffset:
  214.       ret
  215. SetOffset   ENDP
  216.  
  217.  
  218. NextPixel   PROC     NEAR
  219.       mov   cx, BitInc
  220.       mov   bx, Direct
  221.       or    bx, bx
  222.       jz    IncrementBitMask
  223.       add   Offset, cx
  224.       jnc   MouseTest
  225.       inc   Plane
  226.       call  SetATIPlane
  227.       jmp   MouseTest
  228.  
  229. IncrementBitMask:
  230.       ror   BitMask, 1              ; Setup for next pixel
  231.       adc   Offset, 0
  232.       loop  IncrementBitMask
  233.  
  234. MouseTest:
  235.       mov   ax, LinePoints
  236.       and   ax, 03fh
  237.       jnz   ExitNextPixel
  238.       call  CheckMouse
  239.  
  240. ExitNextPixel:
  241.       ret
  242. NextPixel   ENDP
  243.  
  244.  
  245. PUBLIC   _DrawMandelbrot
  246. _DrawMandelbrot    PROC  FAR
  247.       push  bp
  248.       mov   bp, sp
  249.       sub   sp, LastLocal           ; Save room on stack for locals
  250.  
  251.       push  si
  252.       push  di
  253.       push  es
  254.  
  255.       mov   ax, 0a000h
  256.       mov   es, ax
  257.       mov   Offset, 0
  258.       mov   BitMask, 80h
  259.  
  260.       mov   ax, jt
  261.       mov   jInit, ax
  262.       mov   ax, jt + 2
  263.       mov   jInit + 2, ax
  264.  
  265.       xor   ax, ax
  266.       mov   Plane, al
  267.       mov   LowMask, ax
  268.       mov   HighMask, ax
  269.       call  SetATIPlane
  270.       mov   cx, SigDigits
  271.       add   cx, 3                   ; Include sign and integer bits
  272.  
  273. SigDigit:
  274.       stc
  275.       rcr   HighMask, 1
  276.       rcr   LowMask, 1
  277.       loop  SigDigit
  278.  
  279.       call  SetOffset
  280.  
  281. LoopY:
  282.       mov   ax, it
  283.       mov   iInit, ax
  284.       mov   ax, it + 2
  285.       mov   iInit + 2, ax
  286.       mov   ax, PointsPerLine
  287.       mov   LinePoints, ax
  288.  
  289. LoopX:
  290.       mov   ax, Iter
  291.       mov   Iterations, ax
  292.       mov   al, 15
  293.       call  SetPixel
  294.       call  CheckPoint              ; ax := Color
  295.       xor   al, 15                  ; Remove cursor pixel
  296.       call  SetPixel
  297.       call  NextPixel
  298.  
  299.       dec   LinePoints              ; Next line?
  300.       jz    CheckLoopY
  301.  
  302. CheckKey:
  303.       mov   ah, 1
  304.       int   16h
  305.       jz    IncrementX              ; Jump if no keys pressed
  306.       cmp   al, 27                  ; Esc key pressed?
  307.       je    ExitDrawMandelbrot
  308.       mov   ah, 0                   ; Clear keyboard buffer
  309.       int   16h
  310.       jmp   CheckKey
  311.  
  312. IncrementX:
  313.       mov   ax, AspectX
  314.       add   iInit, ax
  315.       mov   ax, AspectX + 2
  316.       adc   iInit + 2, ax
  317.       jmp   LoopX
  318.  
  319. CheckLoopY:
  320.       mov   ax, AspectY
  321.       add   jInit, ax
  322.       mov   ax, AspectY + 2
  323.       adc   jInit + 2, ax
  324.       mov   ax, OffsetInc
  325.       add   Offset, ax
  326.       jnc   CheckScreenLines
  327.       inc   Plane
  328.       call  SetATIPlane
  329.  
  330. CheckScreenLines:
  331.       dec   ScreenLines             ; Done?
  332.       jz    AllDone
  333.       jmp   LoopY
  334.  
  335. AllDone:
  336.       xor   al, al
  337.  
  338. ExitDrawMandelbrot:
  339.       xor   ah, ah
  340.       pop   es
  341.       pop   di
  342.       pop   si
  343.       mov   sp, bp                  ; Restore stack pointer
  344.       pop   bp
  345.       ret
  346. _DrawMandelbrot    ENDP
  347.  
  348. SetPixel    PROC     NEAR
  349.       mov   dx, Direct
  350.       or    dx, dx
  351.       jz    Planer
  352.       mov   di, Offset
  353.       xor   BYTE PTR es:[di], al
  354.       ret
  355.  
  356. Planer:
  357.       mov   ah, al                  ; Setup color
  358.       mov   dx, GraphContPort
  359.       xor   al, al
  360.       out   dx, ax
  361.  
  362.       mov   ax, 0f01h               ; Enable reset of all four planes
  363.       mov   dx, GraphContPort
  364.       out   dx, ax
  365.  
  366.       mov   ax, 1803h               ;Set color logic to 'xor'
  367.       out   dx, ax
  368.  
  369.       mov   ah, BitMask             ; Set bit mask
  370.       mov   al, 8
  371.       out   dx, ax
  372.  
  373.       mov   di, Offset              ; Write pixel
  374.       mov   al, es:[di]
  375.       mov   es:[di], al
  376.  
  377.       mov   dx, GraphContPort       ; Reset register default values
  378.       mov   ax, 1
  379.       out   dx, ax
  380.  
  381.       mov   ax, 3
  382.       out   dx, ax
  383.  
  384.       mov   ax, 0ff08h
  385.       out   dx, ax
  386.       ret
  387. SetPixel    ENDP
  388.  
  389. CheckPoint  PROC  NEAR
  390.       xor   ax, ax                  ; Initialize pattern detection locals
  391.       mov   Lasti, ax               ; Lasti := 0
  392.       mov   Lasti + 2, ax
  393.       mov   Lastj, ax               ; Lastj := 0
  394.       mov   Lastj + 2, ax
  395.       mov   NumRep, ax              ; NumRep := 0
  396.       inc   ax
  397.       mov   LastIter, ax            ; LastIter := 1
  398.       mov   LastRep, ax             ; LastRep  := 1
  399.       inc   ax
  400.       inc   ax
  401.       mov   CheckEvery, ax          ; CheckEver := 3
  402.  
  403.       mov   bx, iInit               ; Copy initial values to i & j
  404.       mov   si, iInit + 2
  405.       mov   cx, jInit
  406.       mov   di, jInit + 2
  407.  
  408.       mov   ax, JuliaFlag
  409.       or    ax, ax
  410.       jnz   NextMandelbrotIteration
  411.  
  412.       mov   Creal, bx
  413.       mov   Creal + 2, si
  414.       mov   Cimag, cx
  415.       mov   Cimag + 2, di
  416.  
  417. NextMandelbrotIteration:
  418.       mov   ax, Iterations
  419.       and   ax, 1fh
  420.       jnz   SkipMouseCheck
  421.       call  CheckMouse
  422.  
  423. SkipMouseCheck:
  424.       call  mult16bit               ; si:bx := i, di:cx := j
  425.       jc    MandelbrotOverflow       ; Calculate (i * j)
  426.       add   ax, ax                  ; dx:ax := 2 * (i * j)
  427.       adc   dx, dx
  428.       add   ax, Cimag               ; Add Initial j
  429.       adc   dx, Cimag + 2
  430.       push  ax                      ; Save new j
  431.       push  dx
  432.       push  di                      ; Save old j
  433.       push  cx
  434.       mov   cx, bx
  435.       mov   di, si
  436.       call  mult16bit               ; Calculate (i * i)
  437.       jc    PopFourOverflow
  438.  
  439.       mov   LastCheck, ax
  440.       mov   LastCheck + 2, dx
  441.  
  442.       pop   cx                      ; Get old j
  443.       pop   di
  444.       push  dx                      ; Save (i * i)
  445.       push  ax
  446.       mov   bx, cx
  447.       mov   si, di
  448.       call  mult16bit               ; Calculate (j * j)
  449.       jc    PopFourOverflow
  450.  
  451.       add   LastCheck, ax
  452.       adc   LastCheck + 2, dx
  453.       jo    PopFourOverflow
  454.       js    PopFourOverflow
  455.  
  456.       pop   bx                      ; si:bx := (i * i)
  457.       pop   si
  458.       sub   bx, ax                  ; si:bx := (i * i) - (j * j)
  459.       sbb   si, dx
  460.       add   bx, Creal               ; Add initial i
  461.       adc   si, Creal + 2           ; si:bx := New i
  462.       pop   di                      ; di:cx := New j
  463.       pop   cx
  464.       jmp   CheckForPattern
  465.  
  466. PopFourOverflow:
  467.       pop   ax                      ; Get rid of saved stack stuff
  468.       pop   ax
  469.  
  470. PopTwoOverflow:
  471.       pop   ax
  472.       pop   ax
  473.  
  474. MandelbrotOverflow:
  475.       push  es
  476.       mov   si, Iter
  477.       sub   si, Iterations
  478.       mov   ax, ColorLog + 2
  479.       mov   es, ax
  480.       add   si, ColorLog
  481.       mov   al, BYTE PTR es:[si]
  482.       pop   es
  483.       and   ax, OverflowMask
  484.       jmp   ExitCheckPoint
  485.  
  486. ; **** Pattern recognition algorithm ****
  487. CheckForPattern:
  488.       mov   ax, LastIter
  489.  
  490.       cmp   si, Lasti
  491.       jne   CheckSampleFreq
  492.  
  493.       cmp   di, Lastj
  494.       jne   CheckSampleFreq
  495.  
  496.       cmp   ax, LastRep             ; if(LastRep == LastIter)
  497.       jne   ResetRepetition
  498.  
  499.       mov   ax, NumRep              ; if(NumRep == RequiredRep)
  500.       inc   NumRep
  501.       cmp   ax, RequiredRep
  502.       jne   DecrementIterations
  503.  
  504.       mov   ax, InSetMask           ; Set color to number of converged points
  505.       or    ax, ax
  506.       jnz   DisplaySet
  507.       xor   ax, ax
  508.       jmp   ExitCheckPoint
  509.  
  510. DisplaySet:
  511.       mov   ax, Iter
  512.       sub   ax, Iterations
  513.       jmp   ExitCheckPoint
  514.  
  515. ResetRepetition:
  516.       mov   LastRep, ax             ; Repetition frequency didn't match
  517.       mov   LastIter, 1
  518.       jmp   DecrementIterations
  519.  
  520. CheckSampleFreq:                    ; Need to look at larger samples?
  521.       inc   LastIter                ; if(LastIter++ == CheckEvery) {
  522.       cmp   ax, CheckEvery
  523.       jne   DecrementIterations
  524.       
  525.       mov   CheckEvery, 1414                ; CheckEvery *= 1.414
  526.       mul   CheckEvery
  527.       mov   CheckEvery, 1000
  528.       div   CheckEvery
  529.       mov   CheckEvery, ax
  530.  
  531.       mov   LastIter, 1             ; Reset last iteration number
  532.  
  533.       mov   Lasti, si
  534.       mov   Lastj, di
  535.  
  536.       mov   NumRep, 0               ; Reset number of repetitions
  537.       jmp   DecrementIterations     ; }
  538.  
  539. ; **** End of pattern recognition stuff ****
  540.  
  541. DecrementIterations:
  542.       dec   Iterations
  543.       jz    UnableToDecide
  544.       jmp   NextMandelbrotIteration
  545.  
  546. UnableToDecide:
  547.       xor   ax, ax
  548.       jmp   ExitCheckPoint
  549.  
  550. ExitCheckPoint:
  551.       ret
  552. CheckPoint  ENDP
  553.  
  554.  
  555. ; Code barrowed from FRACTINT Version 6.1 By Bert Tyler
  556. ; my modifications in capitals
  557.  
  558. mult16bit      PROC  NEAR
  559.    XOR   AX, AX
  560.    mov   temp+4,ax      ; first, zero out the (temporary)
  561.    mov   temp+6,ax      ;  result
  562.  
  563.    mov   sign,al        ; clear out the sign flag
  564.    OR    SI, SI
  565.    jns   mults1         ;  nope
  566.    not   sign        ;  yup.  flip signs
  567.    not   bx       ;   ...
  568.    not   si       ;   ...
  569.    stc            ;   ...
  570.    adc   bx,ax       ;   ...
  571.    adc   si,ax       ;   ...
  572. mults1:  
  573.    OR    DI, DI
  574.    jns   mults2         ;  nope
  575.    not   sign        ;  yup.  flip signs
  576.    not   cx       ;   ...
  577.    not   di       ;   ...
  578.    stc            ;   ...
  579.    adc   cx,ax       ;   ...
  580.    adc   di,ax       ;   ...
  581. mults2:
  582.  
  583.    mov   ax,bx       ; perform BX x CX
  584.    mul   cx       ;  ...
  585.    mov   temp,ax        ;  results in lowest 32 bits
  586.    mov   temp+2,dx      ;  ...
  587.  
  588.    mov   ax,bx       ; perform BX x DI
  589.    mul   di       ;  ...
  590.    add   temp+2,ax      ;  results in middle 32 bits
  591.    adc   temp+4,dx      ;  ...
  592.    XOR   AX, AX
  593.    ADC   TEMP+6,AX
  594.  
  595.    mov   ax,si       ; perform SI * CX
  596.    mul   cx       ;  ...
  597.    add   temp+2,ax      ;  results in middle 32 bits
  598.    adc   temp+4,dx      ;  ...
  599.    XOR   AX, AX
  600.    ADC   TEMP+6,ax
  601.  
  602.    mov   ax,si       ; perform SI * DI
  603.    mul   di       ;  ...
  604.    ADD   AX, TEMP+4
  605.    ADC   DX, TEMP+6
  606.  
  607.    shl   temp+2,1
  608.    rcl   ax,1        ;  ...
  609.    rcl   dx,1        ;  ...
  610.    jc multovfl    ;  (oops.  overflow)
  611.    shl   temp+2,1
  612.    rcl   ax,1        ;  ...
  613.    rcl   dx,1        ;  ...
  614.    jc multovfl    ;  (oops.  overflow)
  615.    shl   temp+2,1
  616.    rcl   ax,1        ;  ...
  617.    rcl   dx,1        ;  ...
  618.    jc multovfl    ;  (oops.  overflow)
  619.    OR    DX, DX
  620.    jS multovfl    ;  ...
  621.  
  622.    cmp   sign, 0
  623.    jZ    multnoovfl
  624.    not   ax       ;  yup.  flip signs.
  625.    not   dx       ;   ...
  626.    ADD   AX, 1
  627.    ADC   DX, 0
  628.  
  629. multnoovfl:          ; normal exit
  630.    clc            ;  no carry
  631.    ret            ; we done.
  632.  
  633. multovfl:            ; overflow exit
  634.    stc            ;  set carry
  635.    ret            ; we done.
  636.  
  637. mult16bit   endp
  638.  
  639. _TEXT    ENDS
  640.  
  641. END
  642.